package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.pojo.Address;
import com.example.demo.service.IAddressService;
import com.example.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地址 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-09
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private IAddressService addressService;
    //查询用户的地址
    @GetMapping("/selid")
    public R get(long id) {
        System.out.println(1);
        List<Address>  addresses=addressService.list(new QueryWrapper<Address>().eq("userid",id));
        Map<String,Object> map =  new HashMap<>();
        map.put("data",addresses);
        map.put("msg","查询成功");
        map.put("code",200);
        return R.ok(map);
    }
    //添加地址
    @PostMapping(value = "/add")
    public R add( @RequestBody Address address){
//        System.out.println(address.getUserid()+ "12312123136138216381");
        addressService.save(address);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","添加成功");
        map.put("code",200);
        return R.ok(map);
    }
    //删除地址
    @PostMapping(value = "/del")
    public R del(long id){
        addressService.removeById(id);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","删除成功");
        map.put("code",200);
        return R.ok(map);
    }
    @GetMapping("/sel")
    public R sel(){
        List<Address> addresses = addressService.list();
        Map<String,Object> map =  new HashMap<>();
        map.put("data",addresses);
        map.put("msg","查询成功");
        map.put("code",200);
        return R.ok(map);
    }
    //修改地址
    @PostMapping("/update")
    public R update(Address address){
        addressService.updateById(address);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","修改成功");
        map.put("code",200);
        return R.ok(map);
    }
}
